﻿ @model FCNS.ERP.Models.DefineClass.DefineHouseCostCalc

@{
    ViewBag.Title = "交租";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@ViewBag.Title</h2> 

<script type="text/javascript">
    function CalcPostString()
    {
        //Id:Price:OldValue:NewValue
        var ps = '';
        $("tr").each(function () {
            if (!isNaN($(this).children().children().val())) {
                ps += ($.trim($(this).children().children().val()) + ":");
                ps += ($.trim($(this).children("td:eq(6)").text()) + ":");
                ps += ($.trim($(this).children("td:eq(4)").text()) + ":");
                ps += ($.trim($(this).children("td:eq(5)").children().val()) + ";");
        }
        });
        return ps;
    }

    function calcSumValue()
    {
        //合计
        var sumAll = 0;
        $("tr").each(function () {
            var sumValue = $(this).children("td:last").text();
            if (!isNaN(sumValue)) {
                sumAll += Number(sumValue);
            }
        });
        $("#Sum").text(sumAll.toFixed(2));
    }

    $(document).ready(function () {
        if ($("#canCalc").attr("value") == "False")
        {
            alert("当前房间有一张未结算单据,请先行处理付款。");
        }

        calcSumValue();

        $(":input").change(function () {
            if (isNaN($(this).val())) {
                alert("请输入数字");
                $(this).val(0);
            }

            //小计
            var c2 = $(this).parent().prev().prev().prev().text();
            var c4 = $(this).parent().prev().text();
            $(this).parent().next().text((Number($(this).val() - c4) * c2).toFixed(2));

            calcSumValue();
        });

        $(":button").click(function () {
            if ($("#canCalc").attr("value") == "False") {
                alert("当前房间有一张未结算单据,请先行处理付款。");
                return;
            }

            var money = Number($("#Sum").text());
            if (money <= 0)
            {
                alert('金额为 0，不能进行结算。');
                return;
            }
            $.post("/House/CostCalc",
                {
                    roomIoOrderNO: $("#RoomIoOrderNO").val(),
                    costString: CalcPostString()
                },
                function (data, status) {
                    if (status == "success") {
                        alert("提交成功，确定跳转到支付页面..." );
                        window.location.replace("/Pay/OrderPay/"+data)
                    }
                    else
                    {
                        alert("提交失败");
                    }
                });
         });
    });
</script>

    @Html.HiddenFor(model => model.RoomIoOrderNO)
<input id="canCalc" type="hidden" value=@Html.Raw(Model.CanCalc) />

<table class="table">
    <tr> 
        <th>
            名称
        </th>
        <th>
            单位 
        </th>
        <th>
            价格
        </th>
        <th>
            摘要 
        </th>
        <th>刻度/值(上次)</th>
        <th>刻度/值(本次)</th>
        <th>小计</th>
    </tr>

@foreach (var item in Model.CostList)
{
    <tr> 
        <td>
            @Html.HiddenFor(modelItem => item.Id)
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UnitName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Summary)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OldValue)
        </td>
        <td>
            @if (item.NewValue == 0)
    {
        <input type="number" name="NewValue" id="NewValue" step="0.01" min=@item.OldValue value=@item.OldValue />
}
else
{
        <input type="number" name="NewValue" id="NewValue" step="0.01" min=@item.OldValue value=@item.NewValue />
}
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sum)
        </td>
    </tr>
}

</table>

<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="button" value="提交" class="btn btn-default" />&nbsp;&nbsp;<h3>合计：<span style="color:red;" id="Sum"></span></h3>
    </div>
</div>
